package com.emasdemo.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.sdk.android.feedback.impl.FeedbackAPI;
import com.alibaba.sdk.android.feedback.util.IUnreadCountCallback;
import com.emasdemo.R;
import com.emasdemo.application.SophixStubApplication;

public class FeedBackActivity extends AppCompatActivity {

    private static final int STORAGE_AND_CAMERA_PERMISSIONS = 2;
    private Handler handler = new Handler(Looper.getMainLooper());
    private TextView tv4;
    private int tag = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);

        initBar();

        FeedbackAPI.setUserNick("醉眼只看倩影");
//        FeedbackAPI.setBackIcon(R.mipmap.back);

        tv4 = (TextView) findViewById(R.id.tv_feed_4);
        findViewById(R.id.tv_feed_a).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("APPKEY--->APPSECRET", SophixStubApplication.appkey + "    " + SophixStubApplication.appSecret);
                tag = 10;
                checkForOpenOrGet(true);

            }
        });

        findViewById(R.id.tv_feed_f).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("APPKEY--->APPSECRET", SophixStubApplication.appkey + "    " + SophixStubApplication.appSecret);
                tag = 11;
                checkForOpenOrGet(true);
            }
        });

        findViewById(R.id.tv_feed_s).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FeedbackAPI.getFeedbackUnreadCount(new IUnreadCountCallback() {
                    @Override
                    public void onSuccess(int i) {
                        Log.i("FeedBack", "s-->" + i);
                        updata("s-->" + i);
                    }

                    @Override
                    public void onError(int i, String s) {
                        Log.i("FeedBack", "e-->" + i + "  " + s);
                        updata("e-->" + i + "  " + s);
                    }
                });
            }
        });
    }

    /**
     * 打开feedback的activity
     * 首先检查需要的权限
     * @param isOpenFeedback
     */
    private void checkForOpenOrGet(boolean isOpenFeedback) {
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, android.Manifest.permission.RECORD_AUDIO)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[] {android.Manifest.permission.READ_EXTERNAL_STORAGE,
                            android.Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO}
                    , STORAGE_AND_CAMERA_PERMISSIONS);
        } else {
            openOrGet(isOpenFeedback);
        }
    }

    /**
     * @param isOpenFeedback 打开网页or获取未读数
     */
    private void openOrGet(final boolean isOpenFeedback) {
        //接入方不需要这样调用, 因为扫码预览, 同时为了服务器发布后能做到实时预览效果, 所有每次都init.
        //业务方默认只需要init一次, 然后直接openFeedbackActivity/getFeedbackUnreadCount即可
        FeedbackAPI.init(getApplication(), SophixStubApplication.appkey, SophixStubApplication.appSecret);//初始化用户反馈


        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isOpenFeedback) {
                    if (tag == 10){
                        FeedbackAPI.openFeedbackActivity();
                    } else {
                        startActivity(new Intent(FeedBackActivity.this, DemoFragmentActivity.class));
                    }

                }
            }
        }, 500);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == STORAGE_AND_CAMERA_PERMISSIONS) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                openOrGet(true);
            } else {
                Toast.makeText(this, "PERMISSION SUCCESS", Toast.LENGTH_SHORT).show();
                updata("PERMISSION SUCCESS");
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    private void updata(final String s) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv4.setText(s);
            }
        });
    }

    private void initBar() {
        findViewById(R.id.bar_img).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FeedBackActivity.this.finish();
            }
        });

        ((TextView)findViewById(R.id.bar_text)).setText("移动用户反馈");
    }
}
